package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 订单系统角色与资源对应关系,对应order_role_res表
 * @author zhoutian   
 * @date 2015-11-26
 */
public class SysRoleRes {
	//角色ID
	private String roleId;
	//@transient 角色名称
	private String roleName;
	//资源ID
	private String resourceId;
	//@transient 资源描述
	private String resourceDesc;
	//创建人
	private String creator;
	//创建时间
	private Date createTime;
	//有效标识,0:无效 1:有效
	private int isValid;

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getResourceDesc() {
		return resourceDesc;
	}

	public void setResourceDesc(String resourceDesc) {
		this.resourceDesc = resourceDesc;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public int getIsValid() {
		return isValid;
	}

	public void setIsValid(int isValid) {
		this.isValid = isValid;
	}

	public SysRoleRes() {}
	
	private static final String select_all_sql = "select order_role_res.role_id as roleId, pub_role.role_name as roleName, order_resource.resource_id as resourceId, order_resource.resource_desc as resourceDesc, order_role_res.is_valid as isValid, order_role_res.creator, order_role_res.createTime"
			+ " from order_role_res inner join pub_role on order_role_res.role_id = pub_role.role_id"
			+ " inner join order_resource on order_role_res.resource_id = order_resource.resource_id";
	
	private static final String update_all_sql = "update order_role_res set role_id = :roleId, resource_id = :resourceId, is_valid = :isValid, creator = :creator, createTime = :createTime";
	
	public void save() throws BusinessException{
		if(!isRoleIdAndResourceIdExist(this.roleId, this.resourceId)){
			String sql = "insert into order_role_res(role_id, resource_id, is_valid, creator, createTime) values(:roleId, :resourceId, :isValid, :creator, :createTime)";
			BaseDAOUtil.updateObject(sql, this);
		} else{
			throw new BusinessException("已存在角色资源关系，不能进行新增操作。");
		}
	}
	
	public void update(){
		String sql = update_all_sql + " where role_id = :roleId and resource_id = :resourceId";
		BaseDAOUtil.updateObject(sql, this);
	}
	
	public void delete(){
		String sql = "delete from order_role_res where role_id = :role_id and resource_id = :resource_id";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("role_id", this.roleId);
		paramMap.put("resource_id", this.resourceId);
		BaseDAOUtil.delete(sql, paramMap);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static List<SysRoleRes> getAll(){
		String sql = select_all_sql + " order by cast (order_role_res.resource_id as numeric) asc";
		return BaseDAOUtil.queryForList(sql, new HashMap(), SysRoleRes.class);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static SysRoleRes getBy(String role_id, String resource_id){
		String sql = select_all_sql + " where order_role_res.role_id = :role_id and order_role_res.resource_id = :resource_id";
		Map paramMap = new HashMap();
		paramMap.put("role_id", role_id);
		paramMap.put("resource_id", resource_id);
		return BaseDAOUtil.queryForObject(sql, paramMap, SysRoleRes.class);
	}
	
	public static boolean isRoleIdAndResourceIdExist(String role_id, String resource_id){
		SysRoleRes sr = getBy(role_id, resource_id);
		return sr != null;
	}
}
